<?php
registerTable("home_leavemessage", new table_home_leavemessage());

class table_home_leavemessage extends Database
{

    public function __construct()
    {
        parent::__construct("home_leavemessage");
    }

    public function fetch_by_lid($lid)
    {
        return $this->fetch_assoc("`lid`=$lid");
    }

    public function fetch_by_uid($uid, $offset, $amount)
    {
        return $this->fetch_all_assoc("`uid`=$uid", "*", "ORDER BY `date` DESC LIMIT $offset,$amount");
    }

    public function fetch_by_target($target, $offset, $amount)
    {
   
        return $this->fetch_all_assoc("`target`=$target", "*", "ORDER BY `date` DESC LIMIT $offset,$amount");
    }

    public function fetch_by_uid_and_target($uid, $target, $offset, $amount)
    {
        return $this->fetch_all_assoc("`uid`=$uid AND `target`=$target", "*", "ORDER BY `date` DESC LIMIT $offset,$amount");
    }

    public function fetch_not_read($target)
    {
        return $this->fetch_all_assoc("`target`=$target AND is_read=0", "*", "ORDER BY `date` DESC");
    }

    public function count_not_read($uid)
    {
        return $this->count_something("`target`=$uid AND `is_read`=0");
    }

    public function read_all($uid)
    {
        return $this->update(array(
            "is_read" => 1
        ), "`target`='$uid'", array(
            "is_read"
        ));
    }
}
?>